/* $Id:contributers.cc $ */
/*
   Copyright (C) 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>
   Part of the Moros Project http://moros.sourceforge.net/

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License version 3.
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY.

   See the COPYING file for more details.
*/

#define DEBUG_ROUTINE "contributers"

#include <sstream>
#include <fstream>
#include "../general/util.h"
#include "../general/log.h"
#include "../general/channel.h"
#include "contributers.h"
#include "army.h"
#include "../general/general.h"

contributers::contributers() {
    army=0;
    amount=0;
}

// Field : army

class army& contributers::getArmy() const { if (army==0) throw nullpointer(); return *army; }
void contributers::setArmy(class army &v) { army=&v; }

// Field : amount

const int contributers::getAmount() const { return amount; }
void contributers::setAmount(const int v) { amount=v; }

void contributers::save(class channel &ch) {
    ch.write("contributers", "");
    ch.write("a", getArmy().getName());
    ch.write("am", getAmount());
}

void contributers::load(class channel &ch) {
    if (ch.key=="a" || ch.key=="army") {
        if (!army_repo.isArmy(ch.value)) {
            dbg_state("link unknown army %s", ch.value.c_str());
            class army rec;
            rec.setName(ch.value);
            army_repo.setArmy(rec);
        }
        setArmy(army_repo.getArmy(ch.value));
        ch.next();
    }
    if (ch.key=="am" || ch.key=="amount") { setAmount(atoi(ch.value.c_str())); ch.next(); }
}

void contributers::xml(ofstream &f, int l, int r) {
    f << ncpy(l, "  ") << "<contributers amount=\"" << getAmount() <<
    "\">" << endl;
    if (r<2) try {
        getArmy().xml(f,l+1,r+1);
    } catch (nullpointer n) {}
    f << ncpy(l, "  ") << "</contributers>" << endl;
}
